<template>
  <div id="app">
    <keep-alive :include="cachePages" :max="66">
      <router-view></router-view>
      <!-- <router-view v-if='$route.meta.keepAlive'></router-view> -->
    </keep-alive>
    <!-- <router-view v-if='!$route.meta.keepAlive'></router-view> -->
  </div>
</template>

<script>
  import StorageKeys from '@/common/const/StorageKeys'
  import {mapState} from 'vuex'
  import NativeAdapt from '@/utils/NativeAdapt'
  import H5Adapt from '@/utils/H5Adapt'
  var TimmerInterval
  NativeAdapt.init()
  H5Adapt.init()
  export default {
    name: 'app',
    data() {
      return {
        pollingTime: 3000,//设置轮询时间
        nameNew:'unknown',
        routerPath:'/',//是否启动定时器，默认不启动
        isOpen:true,//是否打开异地登录检验 默认打开
      }
    },
    computed: {
      ...mapState([
        'cachePages'
      ])
    },
    watch: {
      'cachePages'(newVal){
        console.log(this.cachePages)
      },
      $route(to,from){
        this.routerPath = to.path
        console.log('to.pathto.pathto.pathto.path',to.path);
        if(to.path==='/home'){
          this.getDictValue()
          this.loginDeviceId()
        }
        if(to.path==='/'){
          this.getDictValue()
          this.loginDeviceId()
        }
      },
      pollingTime(newVal){
        //当刷新的值改变时需要重新加载轮询的定时时间
        this.loginDeviceId()
      }
    },
    methods: {
      showAudio() {
        window.audioPlayer = this.$createAudioPlayer({
          $props: {
//            url: this.getVideoSrc(this.detail)
          }
        }).init()
      },
      loginDeviceId(){
        clearInterval(TimmerInterval)
        if(this.routerPath ==='/'){
          return
        }else{
          this.getOffsite()
        }
        TimmerInterval = setInterval(()=>{
          this.getOffsite()
        },this.pollingTime)
       /* setTimeout(()=>{
          this.tools.getDeviceId()
          this.nameNew = '2222'
        },20000)*/
      },
      //异地登录校验
      getOffsite(){
        if(this.isOpen === 'false'){//判断如果关闭验证就不需要验证异地登录
          return false
        }
        if(this.tools.getDeviceId() === 'unknown'){
          return;
        }
        if(this.$localStorage.get('partyInfo') && this.tools.NotIsNull(this.$localStorage.get('partyInfo').userId) && this.$localStorage.get('logoutFlag')=='1'){
          this.pollingTime = this.$localStorage.get('pollingTime')
          this.fetchService.loginDeviceId(this.$localStorage.get('userInfo').id).then(res=>{
            // this.tools.tip("["+res.data+"]["+this.tools.getDeviceId()+"]",1000)
            if(res.code === 0 && res.data!==this.tools.getDeviceId()){
              // this.tools.alert('设备id['+this.tools.getDeviceId()+']返回值['+res.data+']'+'您的账号在别处登录，您已被迫退出，如非本人操作，请修改密码！')
              this.tools.alert('您的账号在别处登录，您已被迫退出，如非本人操作，请修改密码！')
              this.$localStorage.set('logoutFlag', '2')
              this.tools.loginOut()
            }
          })
        }
        console.log('2021.02.26',this.pollingTime)
      },
      //请求字典值
      async getDictValue () {
        let data={
          catKey:'HYTT',
          treeKey:'YDDLSFXYDK'
        }
        await this.fetchService.dictValue(data).then(res => {
          if(res.code === 0){
            this.isOpen = res.data[0].value
            // this.selectList=res.data
          }
        })
      },
      /*定时删除缓存*/
      clearStorage () {
        clearInterval(window.clearStorageTimer)
        window.clearStorageTimer = setInterval(() => {
        }, 3600 * 1000)
      },
      /*引入外部插件*/
      importPlugin () {
        let playerScriptTag = document.getElementById("playerScriptTag");
        // 如果这个tag不存在，则生成相关代码tag以加载代码
        if (playerScriptTag === null) {
          playerScriptTag = document.createElement("script");
          playerScriptTag.type = "text/javascript";
          playerScriptTag.src = './static/js/lib/aliplayer-min.js';
          playerScriptTag.id = "playerScriptTag";
          let s = document.getElementsByTagName("head")[0];
          s.appendChild(playerScriptTag);
        }
      }
    },
    mounted() {
      this.$store.state.userWarningFlag = true;//初始化首页是否弹出用户状态
      this.$nextTick(() => {
        this.importPlugin()
        this.tools.readScore.init()
        this.clearStorage()
        this.$localStorage.set(StorageKeys.APP_RUN_TIME, Date.now())
        let el = document.getElementById('indexLoading')
        if (el) {
          document.querySelector('body').removeChild(el)
        }
      }),
      window['setDeviceID'] = (v) => {
        this.$localStorage.set('deviceIdIOS',v)
      }
    },
    created() {
      try {
        this.fetchService.fetchDynamicHost()
      } catch (e) {}
      this.showAudio()
      if (window.android && window.android.pageLoaded) {
        setTimeout(() => {
          window.android.pageLoaded()
        }, 600)
      }
      this.$store.state.userInfo = this.$localStorage.get('userInfo')
      // this.loginDeviceId()
//      this.tools.readScore.init()
      window.onFetchFail = (() => {
        let failCount = 0
        return () => {
          failCount++
          if (failCount >= 3) {
            failCount = 0
            console.log('网络问题')
          }
        }
      })()
    },
    components: {}
  }
</script>
